package filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;


@WebFilter("/submitForm")
public class FormFilter extends HttpFilter implements Filter {
       
    
    public FormFilter() {
        super();
        
    }

	
	public void destroy() {
		
	}

	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		 String username = request.getParameter("username");
	        String email = request.getParameter("email");

	        if (username == null || username.isEmpty()) {
	            request.setAttribute("error", "用户名不能为空");
	            request.getRequestDispatcher("/error.jsp").forward(request, response);
	            return;
	        }

	        if (email == null || !email.matches("\\w+@\\w+\\.\\w+")) {
	            request.setAttribute("error", "邮箱格式不正确");
	            request.getRequestDispatcher("/error.jsp").forward(request, response);
	            return;
	        }

	        // 放行请求
	        chain.doFilter(request, response);
	    }
		


	
	public void init(FilterConfig fConfig) throws ServletException {
		
	}

}
